﻿using System.Configuration;

namespace SharePointMvc
{
    public enum RouteAccess
    {
        Cache //,
        //Inspect
    }

    public class RoutingCongfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("routeAccessor", IsRequired = true)]
        public RouteAccess RouteAccessor
        {
            get
            {
                return (RouteAccess) this["routeAccessor"];
            }
            set
            {
                this["routeAccessor"] = value;
            }
        }

        [ConfigurationProperty("useDefaultRouteKey", IsRequired = true)]
        public string UseDefaultRouteKey
        {
            get
            {
                return this["useDefaultRouteKey"].ToString();
            }
            set
            {
                this["useDefaultRouteKey"] = value;
            }
        }

        [ConfigurationProperty("routingRedirectionPage",IsRequired = true)]
        public string RoutingRedirectionPage
        {
            get
            {
                return this["routingRedirectionPage"].ToString();
            }
            set
            {
                this["routingRedirectionPage"] = value;
            }
        }

        [ConfigurationProperty("routes")]
        public ConfigurationRoutes ConfigurationRoutes
        {
            get
            {
                return this["routes"] as ConfigurationRoutes;
            }
        }
    }

    public class ConfigurationRoute : ConfigurationElement
    {
        [ConfigurationProperty("url", IsRequired = true)]
        public string Url
        {
            get
            {
                return this["url"] as string;
            }
        }
    }

    public class ConfigurationRoutes : ConfigurationElementCollection
    {
        public ConfigurationRoute this[int index]
        {
            get
            {
                return BaseGet(index) as ConfigurationRoute;
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ConfigurationRoute();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ConfigurationRoute)element).Url;
        }
    }
}